package com.deruan.hotcore.resource.table.model;

import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import org.columba.core.xml.XmlElement;
import org.columba.mail.config.FolderItem;
import org.columba.mail.gui.table.SortingStateObservable;
import com.deruan.hotcore.base.common.bean.ResourceBean;
import com.deruan.hotcore.resource.gui.tree.ResourceTreeNode;
import com.deruan.hotcore.resource.table.ResourceTableView;

public class ResourceTableModelSorter implements ResourceModelVisitor
{
	protected boolean ascending = true;

	protected String sort = new String("Date");

	protected Collator collator;
	
	protected SortingStateObservable sortingStateObservable;

	public ResourceTableModelSorter() {

		setSortingColumn("Date");
		setSortingOrder(true);

		// observable connects the sorting table with the sort menu (View->Sort
		// Messages)
		sortingStateObservable = new SortingStateObservable();
		sortingStateObservable.setSortingState(getSortingColumn(),
				getSortingOrder());

		collator = Collator.getInstance();
	}

	/**
	 * @return
	 */
	public SortingStateObservable getSortingStateObservable() {
		return sortingStateObservable;
	}

	/**
	 * 
	 * This method is used by <class>SortMessagesMenu </class> to generate the
	 * available menuitem entries
	 * 
	 * @return array of visible columns
	 */
	public Object[] getColumns() {
		XmlElement tableElement = FolderItem.getGlobalOptions("options_rs");

		XmlElement columns = tableElement.getElement("columns");

		Vector v = new Vector();

		for (int i = 0; i < columns.count(); i++) {
			XmlElement column = columns.getElement(i);

			String name = column.getAttribute("name");
			v.add(name);
		}

		Object[] result = new String[v.size()];
		result = v.toArray();

		return result;
	}

	public void loadConfig(ResourceTableView view) {

	}

	/**
	 * ***************************** implements TableModelModifier
	 * ******************
	 */

	public String getSortingColumn() {
		return sort;
	}

	public boolean getSortingOrder() {
		return ascending;
	}

	public void setSortingColumn(String str) {
		sort = str;
	}

	public void setSortingOrder(boolean b) {
		ascending = b;
	}

	/**
	 * 
	 * sort the table
	 * 
	 * use selected column and sorting order
	 *  
	 */
	public void sort(ResourceHeaderTableModel tableModel) {
		/*
		 * if (str.equals("In Order Received")) { // do not sort the table, just
		 * use MessageNode rootNode = getRootNode(); } else {
		 */
		ResourceTreeNode rootNode = tableModel.getRootNode();

		// get a list of MessageNode objects of the first
		// hierachy level
		List v = rootNode.getVector();
		if (v == null)
			return;

		// do the sorting
		Collections.sort(v, new ResourceHeaderComparator(getSortingColumn(),
				tableModel.getColumnNumber(getSortingColumn()),
				getSortingOrder()));

		//      notify tree
		//getRealModel().getTreeModel().nodeStructureChanged(getRootNode());
		//}
	}

	class ResourceHeaderComparator implements Comparator {
		protected int column;

		protected boolean ascending;

		protected String columnName;

		public ResourceHeaderComparator(String columnName, int sortCol,
				boolean sortAsc) {
			column = sortCol;
			ascending = sortAsc;
			this.columnName = columnName;
		}

		public int compare(Object o1, Object o2) {
			ResourceTreeNode node1 = (ResourceTreeNode) o1;
			ResourceTreeNode node2 = (ResourceTreeNode) o2;

			ResourceBean header1 = node1.getResourceBean();
			ResourceBean header2 = node2.getResourceBean();

			if ((header1 == null) || (header2 == null)) {
				return 0;
			}

			int result = 0;

		    if ( (columnName.equals("ResourceDate"))) {
				Date d1 = (Date) header1.getPublicDate();
				Date d2 = (Date) header2.getPublicDate();

				if ((d1 == null) || (d2 == null)) {
					result = 0;
				} else {
					result = d1.compareTo(d2);
				}
			}
			if (!ascending) {
				result = -result;
			}

			return result;
		}

		public boolean equals(Object obj) {
			if (obj instanceof ResourceHeaderComparator) {
				ResourceHeaderComparator compObj = (ResourceHeaderComparator) obj;

				return (compObj.column == column)
						&& (compObj.ascending == ascending);
			}

			return false;
		}
	}

	/**
	 * @see org.columba.mail.gui.table.model.ModelVisitor#visit(org.columba.mail.gui.table.model.TreeTableModelInterface)
	 */
	public void visit(ResourceHeaderTableModel realModel) {
		sort(realModel);
	}
}
